package com.miyake.dsp.filter;

import com.miyake.dsp.ComplexArray;
import com.miyake.dsp.filter.FilterSetting.FilterAlgo;
import com.miyake.dsp.filter.FilterSetting.FilterSettingHandler;

public class FilterImplScilabProxy implements Filter {

	private Filter filter;
	
	
	public FilterImplScilabProxy(final FilterSetting setting) {
		setting.addHandler(new FilterSettingHandler() {
			@Override
			public void onFilterAlgoChange(FilterAlgo filterAlgo) {
				createFilter(setting);
			}
		});
		createFilter(setting);
	}

	private void createFilter(FilterSetting setting) {
		ComplexArray source = null;	
		if (filter != null && filter.getSource() != null) {
			source = filter.getSource();
		}
		
		if (setting.getFilterAlgo().equals(FilterAlgo.Fir)) {
			filter = new FirScilab(setting);
		}
		else {
			filter = new IirScilab(setting);
		}
		filter.setSource(source);
	}
	
	@Override
	public void setSource(ComplexArray source) {
		filter.setSource(source);
	}

	@Override
	public ComplexArray getSource() {
		return filter.getSource();
	}

	@Override
	public ComplexArray getProcessed() {
		return filter.getProcessed();
	}

	@Override
	public double[] getMagnitude(int point) {
		return filter.getMagnitude(point);
	}

	@Override
	public double[] getPhase(int point) {
		return filter.getPhase(point);
	}

	@Override
	public Object getSetting() {
		return filter.getSetting();
	}
	
}
